"use strict";

// noinspection ES6ConvertVarToLetConst
/**
 * html模板引擎,为了解决在js中拼接复杂的html文本
 关键字#{key}
 *    <code>
 示例1：
 var ins = new HtmlTemplateEngine();
 ins.putMapper('text','success');
 ins.buildText('<div id="#{text}"><h1>#{text}</h1><h2>#{text}</h2></div>');
 示例2：
 var ins = new HtmlTemplateEngine();
 ins.putMapper('text','success');
 ins.buildText(document.getElementById('123'));
 *    </code>
 * @author 周甜
 * @constructor
 */

var HtmlTemplateEngine = function HtmlTemplateEngine() {
    /**映射数据集*/
    var _mapperData = {};
    /**设置映射数据集*/
    /**添加一对映射数据*/
    var putMapper = function putMapper(key, value) {
        _mapperData[key] = value;
    };
    var setMapper = function setMapper(value) {
        _mapperData = value;
    };
    /**
     加载
     return 替换后的text文本
     */
    var buildText = function buildText(html) {
        for (var key in _mapperData) {
            var value = _mapperData[key];
            var replaceText = "#{" + key + "}";
            html = html.replace(new RegExp(replaceText, "gm"), value);
        }
        return html;
    };
    /**
     加载
     return 元素outerHTML替换后的文本
     */
    var buildElement = function buildElement(element) {
        var html;
        if (element instanceof Element) {
            html = element.outerHTML;
            html = buildText(html);
        }
        return html;
    };
    this.putMapper = putMapper;
    this.setMapper = setMapper;
    this.buildElement = buildElement;
    this.buildText = buildText;
};